package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.incar.chuxing.R;
import com.incar.chuxing.adapter.GuideAdapter;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.utils.SharedPreferencesKey;
import com.incar.chuxing.utils.SharedPreferencesUtils;
import com.incar.chuxing.utils.VerControlUtils;

import java.util.ArrayList;
import java.util.List;


public class GuideActivity extends BaseActivity {
    private Context mContext = this;
    private ViewPager viewPager_guide;
    private RadioGroup radioGroup_guide;
    private TextView tv_guide_know;
    private RelativeLayout progress_bar;
    private GuideAdapter adapter = null;
    private List<ImageView> totalList = new ArrayList<>();
    private TypedArray array = null;

    private RadioButton[] arrRadioButton = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    public void initView() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        tv_guide_know = (TextView) findViewById(R.id.tv_guide_know);
        progress_bar = (RelativeLayout) findViewById(R.id.progress_bar);
        tv_guide_know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progress_bar.setVisibility(View.VISIBLE);
                startActivity(new Intent(mContext, MainActivity.class));
                finish();
            }
        });
        radioGroup_guide = (RadioGroup) findViewById(R.id.radioGroup_guide);
        viewPager_guide = (ViewPager) findViewById(R.id.viewPager_guide);
        adapter = new GuideAdapter(totalList);
        viewPager_guide.setAdapter(adapter);
        initDots();
        viewPager_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                arrRadioButton[position].setChecked(true);
                if (position == (adapter.getCount() - 1)) {
                    radioGroup_guide.setVisibility(View.GONE);
                    tv_guide_know.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.guide_button_in));
                    tv_guide_know.setVisibility(View.VISIBLE);
                } else {
                    radioGroup_guide.setVisibility(View.VISIBLE);
                    tv_guide_know.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {
        VerControlUtils verControlUtils = new VerControlUtils(mContext);
        SharedPreferencesUtils.getInstance(mContext).putIntValue(SharedPreferencesKey.OLD_VERSION_CODE, verControlUtils.getVersionCode());
        array = getResources().obtainTypedArray(R.array.arrImages);
        for (int i = 0; i < array.length(); i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Drawable drawable = array.getDrawable(i);
            imageView.setImageDrawable(drawable);
            totalList.add(imageView);
        }
    }

    private void initDots() {
        arrRadioButton = new RadioButton[array.length()];
        for (int i = 0; i < array.length(); i++) {
            RadioButton radioButton = new RadioButton(mContext);
            radioButton.setButtonDrawable(R.drawable.dots);
            radioButton.setPadding(15, 0, 15, 0);
            radioGroup_guide.addView(radioButton);
            arrRadioButton[i] = radioButton;
        }
        //让radioButton第一个被勾选
        arrRadioButton[0].setChecked(true);
        radioGroup_guide.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < arrRadioButton.length; i++) {
                    if (arrRadioButton[i].getId() == checkedId) {
                        viewPager_guide.setCurrentItem(i);
                    }
                }
            }
        });
    }

}
